{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": "import numpy as np",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 一维数组\n",
    "arr1 = np.array(['a','b','c','d','e','f'])\n",
    "print(arr1)\n",
    "# 二维数组\n",
    "arr2 = np.array([[1,2,3],[4,5,6,]])\n",
    "arr2"
   ],
   "id": "eaa787413d333038",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 全零数组\n",
    "arr3 = np.zeros((2,3),dtype=int) # 默认类型是float\n",
    "print(arr3)\n",
    "# 全一数组\n",
    "arr4 = np.ones((3,4),dtype=int)\n",
    "print(arr4)\n",
    "# 空数组 np.empty 仅分配内存空间，不初始化元素，后续要手动初始化\n",
    "arr5 = np.empty((1,5),dtype=int)\n",
    "print(arr5)\n",
    "# 范围数组\n",
    "arr6  = np.arange(0,15,3) # [0,15) 步长3\n",
    "print(arr6)"
   ],
   "id": "228dc65ab1bedaed",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 等差数组\n",
    "arr7 = np.linspace(1,2,5) # [1,2],5个元素\n",
    "print(arr7)\n",
    "# 单位矩阵\n",
    "arr8 =np.eye(3,dtype=int)\n",
    "print(arr8)\n",
    "# 随机数组\n",
    "arr9 = np.random.randint(0,10,3) # [0,10)生成三个\n",
    "print(arr9)"
   ],
   "id": "a10fd42e2f6b1d6",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 数组属性\n",
    "arr10 = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "print(arr10.shape) # 数组形状\n",
    "print(arr10.ndim) # 数组维度\n",
    "print(arr10.size) # 数组大小\n",
    "print(arr10.dtype) # 数组元素类型\n",
    "print(arr10.itemsize)  # 数组元素字节数"
   ],
   "id": "e3ca963e3baa51a5",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 索引\n",
    "print(arr10[0,1])\n",
    "print(arr10[1])\n",
    "# 切片\n",
    "print(arr10[:,2])\n",
    "# 布尔索引\n",
    "print(arr10[arr10>5])\n",
    "# 花式索引\n",
    "print(arr10[[0,2],[1,2]])  # [0,1]   [2,2]"
   ],
   "id": "6193ac80d2a582a0",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 基础运算\n",
    "a = np.array([4,5,6])\n",
    "b = np.array([1,4,9])\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)\n",
    "print(a**2)\n",
    "print(a//b)\n",
    "print(a%b)\n",
    "print(a**b)\n",
    "print(np.sqrt(b))  # 开根号"
   ],
   "id": "8378896aacc18783",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# numpy数组的广播机制  处理不同形状数组之间算术运算\n",
    "# 广播机制会自动调整（“扩展”）其中一个或两个数组的形状，使它们能够进行元素级运算（如加减乘除等），而无需显式复制数据。\n",
    "arr11 = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "arr12 = np.array([1,2,4])\n",
    "print(arr11+arr12)\n",
    "print(arr12+11)"
   ],
   "id": "f7237d0ca76253ca",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 统计函数\n",
    "print(arr11.sum())\n",
    "print(arr11.mean()) # 平均数\n",
    "print(arr11.max())\n",
    "print(arr11.min(axis=1)) # 1是按行找最小 0是按列找最小\n",
    "print(arr11.std()) # 标准差"
   ],
   "id": "e6bef61b8856743",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 数组重组\n",
    "arr13 = np.arange(12)\n",
    "print(arr13)\n",
    "temp = arr13.reshape(3,4)\n",
    "print(temp,temp.shape)\n",
    "# 展平数组\n",
    "temp1 = temp.flatten()\n",
    "print(temp1,temp1.shape)\n",
    "# 转置矩阵\n",
    "temp2 = temp.T\n",
    "print(temp2,temp2.shape)"
   ],
   "id": "c132ff86de86269a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 合并与分割\n",
    "arr14 = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "arr15 = np.array([[10,11,12],[13,14,15],[16,17,18]])\n",
    "# 合并\n",
    "temp3 = np.vstack((arr14,arr15)) # 垂直合并\n",
    "temp4 = np.hstack((arr14,arr15)) # 水平合并\n",
    "print(temp3)\n",
    "print(temp4)\n",
    "# 分割\n",
    "temp5 = np.split(arr14,3)\n",
    "print(temp5)"
   ],
   "id": "73effe7605867fe",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
